package com.yimi.yinhepda.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 000432
 */

public class MakeXml {
  private final static String rootPath = "C:\\Users\\lj\\Desktop\\layoutroot\\values-{0}x{1}\\";

  private final static float dw = 320f;
  private final static float dh = 480f;

  private final static String WTemplate = "<dimen name=\"xdp{0}\">{1}px</dimen>\n";
  private final static String HTemplate = "<dimen name=\"ydp{0}\">{1}px</dimen>\n";

  public static void main(String[] args) {
    makeString(640, 480);
    //makeString(320, 480);
    //makeString(480,800);
    //makeString(480, 854);
    //makeString(540, 960);
    //makeString(600, 1024);
    //makeString(720, 1184);
    //makeString(720, 1196);
    //makeString(720, 1280);
    //makeString(768, 1024);
    //makeString(800, 1280);
    //makeString(1080, 1812);
    //makeString(1080, 1920);
    //makeString(1440, 2560);
  }

  public static void makeString(int w, int h) {

    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
    sb.append("<resources>");
    float cellw = w / dw;
    for (int i = 1; i < 320; i++) {
      sb.append(WTemplate.replace("{0}", i + "").replace("{1}", change(cellw * i) + ""));
    }
    sb.append(WTemplate.replace("{0}", "320").replace("{1}", w + ""));
    sb.append("</resources>");

    StringBuffer sb2 = new StringBuffer();
    sb2.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
    sb2.append("<resources>");
    float cellh = h / dh;
    for (int i = 1; i < 480; i++) {
      sb2.append(HTemplate.replace("{0}", i + "").replace("{1}", change(cellh * i) + ""));
    }
    sb2.append(HTemplate.replace("{0}", "480").replace("{1}", h + ""));
    sb2.append("</resources>");

    String path = rootPath.replace("{0}", h + "").replace("{1}", w + "");
    File rootFile = new File(path);
    if (!rootFile.exists()) {
      rootFile.mkdirs();
    }
    File layxFile = new File(path + "dimens_x.xml");
    File layyFile = new File(path + "dimens_y.xml");
    try {
      PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
      pw.print(sb.toString());
      pw.close();
      pw = new PrintWriter(new FileOutputStream(layyFile));
      pw.print(sb2.toString());
      pw.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }

  public static float change(float a) {
    int temp = (int) (a * 100);
    return temp / 100f;
  }

  /**
   * 生成字体大小适配xml文件
   */
  public static void gen() {

    File file = new File("./app/src/main/res/values/dimens.xml");
    BufferedReader reader = null;
    StringBuilder sw480 = new StringBuilder();
    StringBuilder sw600 = new StringBuilder();
    StringBuilder sw720 = new StringBuilder();
    StringBuilder sw800 = new StringBuilder();
    StringBuilder w820 = new StringBuilder();

    try {
      System.out.println("生成不同分辨率：");
      reader = new BufferedReader(new FileReader(file));
      String tempString;
      int line = 1;
      // 一次读入一行，直到读入null为文件结束

      while ((tempString = reader.readLine()) != null) {

        if (tempString.contains("</dimen>")) {
          //tempString = tempString.replaceAll(" ", "");
          String start = tempString.substring(0, tempString.indexOf(">") + 1);
          String end = tempString.substring(tempString.lastIndexOf("<") - 2);
          int num = Integer.valueOf(tempString.substring(tempString.indexOf(">") + 1, tempString.indexOf("</dimen>") - 2));

          sw480.append(start).append((int) Math.round(num * 0.6)).append(end).append("\n");
          sw600.append(start).append((int) Math.round(num * 0.75)).append(end).append("\n");
          sw720.append(start).append((int) Math.round(num * 0.9)).append(end).append("\n");
          sw800.append(tempString).append("\n");
          w820.append(tempString).append("\n");
        } else {
          sw480.append(tempString).append("\n");
          sw600.append(tempString).append("\n");
          sw720.append(tempString).append("\n");
          sw800.append(tempString).append("\n");
          w820.append(tempString).append("\n");
        }
        line++;
      }
      reader.close();
      System.out.println("<!--  sw480 -->");
      System.out.println(sw480);
      System.out.println("<!--  sw600 -->");
      System.out.println(sw600);

      System.out.println("<!--  sw720 -->");
      System.out.println(sw720);
      System.out.println("<!--  sw800 -->");
      System.out.println(sw800);

      String rootPath = "C:\\Users\\Administrator\\Desktop\\layoutroot\\";

      File sw480fileDir = new File(rootPath + "values-sw480dp");
      if (!sw480fileDir.exists()) {
        sw480fileDir.mkdirs();
      }
      File sw600fileDir = new File(rootPath + "values-sw600dp");
      if (!sw600fileDir.exists()) {
        sw600fileDir.mkdirs();
      }
      File sw720fileDir = new File(rootPath + "values-sw720dp");
      if (!sw720fileDir.exists()) {
        sw720fileDir.mkdirs();
      }
      File sw800fileDir = new File(rootPath + "values-sw800dp");
      if (!sw800fileDir.exists()) {
        sw800fileDir.mkdirs();
      }
      File w820fileDir = new File(rootPath + "values-w820dp");
      if (!w820fileDir.exists()) {
        w820fileDir.mkdirs();
      }

      String fileName = "\\dimens.xml";

      String sw480file = sw480fileDir.getPath() + fileName;
      String sw600file = sw600fileDir.getPath() + fileName;
      String sw720file = sw720fileDir.getPath() + fileName;
      String sw800file = sw800fileDir.getPath() + fileName;
      String w820file = w820fileDir.getPath() + fileName;
      writeFile(sw480file, sw480.toString());
      writeFile(sw600file, sw600.toString());
      writeFile(sw720file, sw720.toString());
      writeFile(sw800file, sw800.toString());
      writeFile(w820file, w820.toString());
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
    }
  }

  public static void writeFile(String file, String text) {
    PrintWriter out = null;
    try {
      out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
      out.println(text);
    } catch (IOException e) {
      e.printStackTrace();
    }

    out.close();
  }
}
